<?php
  class UnitService{  
  
   function getUnitById($select, $uid, $provinceIdArray, $districtIdArray, $tambonIdArray, $select_moo){
			
			if(in_array($select, $tambonIdArray)){	
				$where = array();
	
				
        $sql = "
                 SELECT
                  pn_degis_contact.aco_unit,
                  COUNT(pn_degis_contact.aco_unit) AS count_population,
                  pn_degis_tumbol.atb_lat,
                  pn_degis_tumbol.atb_lon
                 FROM 
                  samuidemap.pn_degis_tumbol,
                  samuidemap.pn_degis_contact
                WHERE  
                  pn_degis_contact.aco_tumbol_id = $select AND
                  pn_degis_tumbol.atb_id = $select  
								AND
								  pn_degis_contact.aco_addr_moo = $select_moo		                  
                GROUP BY 
                  pn_degis_contact.aco_unit
                ORDER BY 
                  pn_degis_contact.aco_unit";
                  
          //echo $sql; exit;
          $column = array('unit', 'count_population', 'lat', 'lon');
          $result = DBUtil::executeSQL($sql);
          $objectArray = DBUtil::marshallObjects ($result, $column);

          //print_r ($objectArray);
					if($objectArray){
						//return $objectArray; 
						//echo "LLLL"; exit;
						$this->setResponse($uid, $provinceIdArray, $districtIdArray, $tambonIdArray, $objectArray, $select, $select_moo);
					}else{
						$this->setResponseNoData();
						//echo "Nodata";
					}
				}else{
					echo '<?xml version="1.0" encoding="UTF-8"?>
								<datas>
									<data id=0>
										<title>Permission denied , you cannot access this unit.</title>
									</data>
								</datas>';    					
				}
  	}
  	
  	
  	
  	function setResponse($uid, $provinceIdArray, $districtIdArray, $tambonIdArray, $objectArray, $select, $select_moo){
  	
  	
  	//Sample $nextxml
	  //http://localhost/zk128/index.php?module=VoteDataCenter&type=service&func=place&province=84&district=8404&tambon=840401,840402,840403,840404,840405,840406&uid=2&select=840406&select_moo=1
	  
	  	//echo "http://localhost/zk128/index.php?module=VoteDataCenter&type=service&func=place&province=84&district=8404&tambon=840401,840402,840403,840404,840405,840406&uid=2&select=840406&select_moo=1";

      $accessArray['province_access'] = implode(",", $provinceIdArray);
      $accessArray['district_access'] = implode(",", $districtIdArray);
      $accessArray['tambon_access'] = implode(",", $tambonIdArray);
      
      // Gen xml
      $xml = new DOMDocument('1.0', 'UTF-8');
      $xml->formatOutput = true;
      $xml_node_datas = $xml->createElement("datas");
      $xml->appendChild( $xml_node_datas );

      foreach($objectArray as $key => $val){
        $id = $select.$select_moo.$val['unit'];
        $villageId = "un".$id;
        $villageName = "หน่วยที่ ".$val['unit'];
          $xml_node_data = $xml->createElement("data");
          $xml_node_datas->appendChild( $xml_node_data );
          $xml_node_data->setAttribute("id", $villageId);
            $xml_node_loop = $xml->createElement("loop", 0);
            $xml_node_data->appendChild( $xml_node_loop );           
            $xml_node_title = $xml->createElement("title",$villageName );
            $xml_node_data->appendChild( $xml_node_title );
            $xml_node_population = $xml->createElement("population",$val['count_population']);
            $xml_node_data->appendChild( $xml_node_population );
            $xml_node_latitude = $xml->createElement("latitude",$val['lat']);
            $xml_node_data->appendChild( $xml_node_latitude );
            $xml_node_longitude = $xml->createElement("longitude",$val['lon']);
            $xml_node_data->appendChild( $xml_node_longitude );
            $xml_node_span = $xml->createElement("span","0.2");
            $xml_node_data->appendChild( $xml_node_span );
            $xml_node_infopath = $xml->createElement("infopath","information.xml");
            $xml_node_data->appendChild( $xml_node_infopath );
            $xml_node_reportpath = $xml->createElement("reportpath","http://data.votethailand.com/report_no_data.xml");
            $xml_node_data->appendChild( $xml_node_reportpath );

            $nextxml = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteDataCenter&amp;type=service&amp;func=place&amp;province=".$accessArray['province_access']."&amp;district=".$accessArray['district_access']."&amp;tambon=".$accessArray['tambon_access']."&amp;uid=".$uid."&amp;select=".$select."&amp;select_moo=".$select_moo."&amp;select_unit=".$val['unit'];
            // Check For Sample ID
            if ($uid == '6' && $key>='5'){ 
               $xml_node_xmlpath = $xml->createElement("xmlpath","");
            }else{
              $xml_node_xmlpath = $xml->createElement("xmlpath",$nextxml);
            }
            $xml_node_data->appendChild( $xml_node_xmlpath );
            $xml_node_xmlupdate = $xml->createElement("xmlupdate","LocalityData");
            $xml_node_data->appendChild( $xml_node_xmlupdate );
            $xml_node_date = $xml->createElement("date","2010-11-11 20:26:56");
            $xml_node_data->appendChild( $xml_node_date );
      }

      echo $xml->saveXML();
      pnShutDown();
  	}
  }
?>